<?php

/**
 * 分类管理
 */
namespace app\site\model;

use app\system\model\SystemModel;

class SiteLinkClassModel extends SystemModel {

    protected $infoModel = [
        'pri' => 'class_id',
        'validate' => [
            'name' => [
                'len' => ['1, 20', '分类名称输入不正确!', 'must', 'all'],
            ],
        ],
        'format' => [
            'name' => [
                'function' => ['htmlspecialchars', 'all'],
            ],
            'description' => [
                'function' => ['htmlspecialchars', 'all'],
            ],
        ],
        'into' => '',
        'out' => '',
    ];

    public function _editBefore($data) {
        if ($data['parent_id'] == $data['class_id']) {
            $this->error = '您不能将当前分类设置为上级分类!';
            return false;
        }
        $cat = $this->loadList('', 0, '', $data['class_id']);
        if($cat) {
            foreach ($cat as $vo) {
                if ($data['parent_id'] == $vo['class_id']) {
                    $this->error = '不可以将上一级分类移动到子分类';
                    return false;
                }
            }
        }
        return $data;
    }

    /**
     * 获取分类树
     * @return array
     */
    public function loadList($where = [], $limit = 0, $order = '', $pid = 0) {
        $list = parent::loadList($where, 0, $order = 'sort asc, class_id asc');
        $class = new \dux\lib\Category(['class_id', 'parent_id', 'name', 'cname']);
        return $class->getTree($list, $pid);
    }

    

}